package com.zkingsoft.rhi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkingsoft.rhi.entity.StateOwnedUnits;
import com.zkingsoft.rhi.handler.Result;
import com.zkingsoft.rhi.service.StateOwnedUnitsService;
import com.zkingsoft.rhi.utils.MyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 国有单位表 前端控制器
 * </p>
 *
 * @author xieguo
 * @since 2020-11-04
 */
@Api(tags = "国有单位")
@RestController
@RequestMapping("/rhi/units")
public class StateOwnedUnitsController {

    @Autowired
    StateOwnedUnitsService sous;

    @Autowired
    MyUtils myUtils;

    /**
     * @param sou
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：查询单位，可搜索
     * @date 2020/11/10 11:24
     */
    @ApiOperation(value = "查询单位，可搜索")
    @PostMapping("/select")
    public JSONObject selectUnits(@RequestBody JSONObject sou) {
        Integer offset = (Integer) sou.get("offset");
        Integer limit = (Integer) sou.get("limit");
        String name = (String) sou.get("name");
        if (offset != null && limit != null && offset > 0 && limit > 0) {
            Page<StateOwnedUnits> souPage = new Page<>(offset, limit);
            QueryWrapper<StateOwnedUnits> souqw = new QueryWrapper<>();
            if (null != name) {
                souqw.like("name", name);
            }
            IPage<StateOwnedUnits> page = sous.page(souPage, souqw);
            Integer total = (int) page.getTotal();
            return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", page.getRecords(), total);
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "参数错误");
        }
    }

    /**
     * @param sou
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：新增单位
     * @date 2020/11/10 11:24
     */
    @ApiOperation(value = "新增单位")
    @PostMapping("/insert")
    public JSONObject insertUnits(@RequestBody StateOwnedUnits sou) {
        QueryWrapper<StateOwnedUnits> souqw = new QueryWrapper<>();
        souqw.eq("number", sou.getNumber());
        StateOwnedUnits stateOwnedUnits = sous.getOne(souqw);
        if (ObjectUtil.isNotEmpty(stateOwnedUnits)) {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "单位编号已存在");
        }

        QueryWrapper<StateOwnedUnits> souqwName = new QueryWrapper<>();
        QueryWrapper<StateOwnedUnits> name = souqwName.eq("name", sou.getName());
        List<StateOwnedUnits> list = sous.list(name);
        if (ObjectUtil.isNotEmpty(list)){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"单位名称不能重复");
        }

        boolean save = sous.save(sou);
        if (save) {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "保存成功");
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "保存失败");
        }
    }

    /**
     * @param ids
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：删除单位，可批量删除
     * @date 2020/11/10 11:24
     */
    @ApiOperation(value = "删除单位")
    @GetMapping("/delete/{ids}")
    public JSONObject deleteUnits(@PathVariable("ids") String ids) {
        List<Integer> serverIds = myUtils.handleIds(ids);
        boolean delete = sous.removeByIds(serverIds);
        if (delete) {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "删除成功");
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "删除失败");
        }
    }

    /**
     * @param sou
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：更新单位
     * @date 2020/11/10 11:25
     */
    @ApiOperation(value = "更新单位")
    @PostMapping("/update")
    public JSONObject updateUnits(@RequestBody StateOwnedUnits sou) {

        StateOwnedUnits oldOwnedUnits = sous.getById(sou.getId());
        if (!oldOwnedUnits.getNumber().equals(sou.getNumber())) {
            QueryWrapper<StateOwnedUnits> souqw = new QueryWrapper<>();
            souqw.eq("number", sou.getNumber());
            StateOwnedUnits stateOwnedUnits = sous.getOne(souqw);
            if (ObjectUtil.isNotEmpty(stateOwnedUnits)) {
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "单位编号已存在");
            }
        }

        if (!oldOwnedUnits.getName().equals(sou.getName())){
            QueryWrapper<StateOwnedUnits> wrapper = new QueryWrapper<>();
            wrapper.eq("name",sou.getName());
            List<StateOwnedUnits> list = sous.list(wrapper);
            if (ObjectUtil.isNotEmpty(list)){
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"单位名称已存在");
            }
        }

        boolean update = sous.updateById(sou);
        if (update) {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "更新成功");
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "更新失败");
        }
    }

    @ApiOperation(value = "回显")
    @GetMapping("/selectUnitByIds/{id}")
    public JSONObject selectUnitById(@PathVariable String id) {
        StateOwnedUnits ownedUnits = sous.getById(id);
        return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", ownedUnits, null);
    }
}

